package com.niit.mall.address.controller;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.niit.mall.address.config.AddressProperties;
import com.niit.mall.address.domain.dto.AddressDTO;
import com.niit.mall.address.domain.po.Address;
import com.niit.mall.address.domain.vo.AddressVO;
import com.niit.mall.address.service.IAddressService;
import com.niit.mall.common.api.CommonResult;
import com.niit.mall.common.utils.BeanUtils;
import com.niit.mall.common.utils.CollUtils;
import com.niit.mall.common.utils.UserContext;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.Collections;
import java.util.List;

/**
 * 地址服务
 * @module pardo_mall
 * @BelongsProject: pardo_mall_backend
 * @BelongsPackage: com.niit.mall.address.controller;
 * @Author: ChuXuan
 * @CreateTime: 2025-05-22  00:12
 * @Description: 地址请求
 * @Version: 1.0
 */
@RestController
@RequestMapping("/address")
@Slf4j
@RequiredArgsConstructor
public class AddressController {
	
	private final IAddressService addressService;
	
	private final AddressProperties addressProperties;
	
	/**
	 * 服务器端口号
	 */
	@Value("${server.port}")
	private String port;
	
	/**
	 * 根据用户ID查询地址列表
	 *
	 * @return 用户ID对应的地址列表
	 */
	@GetMapping("{userId}")
	public List<AddressVO> queryAddressByUserId(@PathVariable("userId") Long userId) {
		List<Address> addressList = addressService.query().eq("user_id", userId).list();
		if(CollUtils.isEmpty(addressList)){
			return CollUtils.emptyList();
		}
		return BeanUtils.copyList(addressList, AddressVO.class);
	}
	
	/**
	 * 更新地址信息的方法
	 *
	 * @param addressDTO 请求体中的地址数据传输对象
	 * @return 返回通用结果对象，包含更新操作的成功或失败状态
	 */
	@PutMapping
	public CommonResult updateAddress(@RequestBody AddressDTO addressDTO) {
		Long  userId = UserContext.getUser();
		// DTO转PO
		Address address = BeanUtil.copyProperties(addressDTO, Address.class);
		LocalDate updateDate = LocalDate.now();
		address.setUpdateDate(updateDate);
		QueryWrapper<Address> queryWrapper = new QueryWrapper<Address>()
											.eq("user_id", userId)
											.eq("id", address.getId());
		boolean update = addressService.update(address, queryWrapper);
		return CommonResult.success(update);
	}
	
	/**
	 * 新增地址信息的方法
	 *
	 * @param addressDTO 包含地址信息的DTO对象
	 * @return 通用结果对象，表明操作是否成功
	 */// 新增地址
	@PostMapping()
	public CommonResult addAddress(@RequestBody AddressDTO addressDTO){
		Long userId = UserContext.getUser();
		Address address = BeanUtil.copyProperties(addressDTO, Address.class);
		address.setUserId(userId);
		LocalDate createDate = LocalDate.now();
		address.setCreateDate(createDate);
		LocalDate updateDate = LocalDate.now();
		address.setUpdateDate(updateDate);
		boolean save = addressService.save(address);
		return CommonResult.success(save);
	}
	
	/**
	 * 根据ID删除地址
	 *
	 * @param id 地址ID
	 * @return 删除操作的结果，包含操作是否成功的状态
	 */// 删除地址
	@DeleteMapping("{addressId}")
	public CommonResult removeAddress(@PathVariable("addressId") Long id) {
		boolean remove = addressService.removeById(id);
		return CommonResult.success(remove);
	}


	/**
	 * 设置默认地址的方法
	 *
	 * @param addressId 地址ID
	 * @return 返回通用结果对象，包含设置默认地址操作的成功或失败状态
	 */
	@PutMapping("{addressId}/default")
	public CommonResult setDefaultAddress(@PathVariable("addressId") Long addressId) {
		Long userId = UserContext.getUser();
		// 先将原来的默认地址改为0
		QueryWrapper<Address> queryWrapper = new QueryWrapper<Address>()
				.eq("user_id", userId)
				.eq("is_default", 1);
		Address oldDefaultAddress = addressService.getOne(queryWrapper);
		if (oldDefaultAddress != null) {
			oldDefaultAddress.setIsDefault(0);
			addressService.updateById(oldDefaultAddress);
		}
		// 再将新的地址设置为默认地址
		Address newDefaultAddress = addressService.getById(addressId);
		if (newDefaultAddress != null) {
			newDefaultAddress.setIsDefault(1);
			addressService.updateById(newDefaultAddress);
		}
		return CommonResult.success(true);

	}
}
